package com.bankcomm.ui;

import java.util.ArrayList;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bankcomm.BankcommApplication;
import com.bankcomm.R;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.views.IHeightListView;
import com.bankcomm.ui.views.LPBottomBar;
import com.bankcomm.util.LPUtils;

/**
 * BCMNewsCenter.java
 * @author cheng.l 2013-6-13 下午01:42:03
 */
public class BCMNewsCenter extends BaseViewCopy implements OnClickListener{

	private IHeightListView newsList,favsList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.boc_news_center);

		init();
	}

	private void init() {
		LPUtils.scaledTHeight((LinearLayout)findViewById(R.id.top));
		((LPBottomBar)findViewById(R.id.bottom_bar)).setSelectTab(1);
		
		newsList = (IHeightListView)findViewById(R.id.newsList);
		favsList = (IHeightListView)findViewById(R.id.favList);
		setListViewPre(newsList);
		setListViewPre(favsList);
		
		newsList.setAdapter(new ListAdapter(LPMid.getInstance().um_.data.myNews));
		favsList.setAdapter(new ListAdapter(LPMid.getInstance().um_.data.favNews));
		
		Button tl = (Button)findViewById(R.id.newsTitleL);
		Button tr = (Button)findViewById(R.id.newsTitleR);
		
		LPUtils.setTitleButtonWH(tl);
		LPUtils.setTitleButtonWH(tr);
		
		tl.setOnClickListener(this);
		tr.setOnClickListener(this);
		
		newsList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Map<String, Object> map = LPMid.getInstance().um_.data.myNews.get(arg2);
				String url = (String) map.get("url");
				if(null != url && !"".equalsIgnoreCase(url)){
					LPMid.getInstance().um_.gotoEmpViewFromHome(BCMNewsCenter.this, url,"");
				}
			}
		});
		
		favsList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Map<String, Object> map = LPMid.getInstance().um_.data.favNews.get(arg2);
				String url = (String) map.get("url");
				if(null != url && !"".equalsIgnoreCase(url)){
					LPMid.getInstance().um_.gotoEmpViewFromHome(BCMNewsCenter.this, url,"");
				}
			}
		});
	}
	
	private void setListViewPre(IHeightListView listView){
		listView.setScrollingCacheEnabled(false);
		listView.setVerticalFadingEdgeEnabled(false);
		listView.setVerticalScrollBarEnabled(true);
		listView.setDivider(this.getResources().getDrawable(R.drawable.divider_horizontal_timeline));
		listView.setDividerHeight(2);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.newsTitleL:
			this.finish();
			break;
		case R.id.newsTitleR:
			LPMid.getInstance().um_.backToHome(this, 1);
			break;
		}
	}
	
	@Override
	public void finish() {
		newsList = null;
		favsList = null;
		BankcommApplication.getInstance().removeActivity(this);
		super.finish();
	}
	
	public class ListAdapter extends BaseAdapter{
		private ArrayList<Map<String, Object>> lists;
		public ListAdapter(ArrayList<Map<String, Object>> list){
			this.lists = list;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lists.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return lists.get(arg0);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) { 
	            convertView = LayoutInflater.from(getBaseContext()).inflate(R.layout.boc_news_item, null); 
	            holder = new ViewHolder();
	            holder.date = (TextView)convertView.findViewById(R.id.date);
	            holder.content = (TextView)convertView.findViewById(R.id.content);
	            convertView.setTag(holder);
	        } else{
	        	holder = (ViewHolder) convertView.getTag();
	        }
			
			Map<String,Object> map = lists.get(position);
			String content = (String) map.get("content");
			if(null != content){
				holder.content.setText(content);
				holder.date.setVisibility(View.GONE);
			}
			return convertView;
		}
	}
	
	public class ViewHolder{
		public TextView date;
		public TextView content;
	}
}
